package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.ActivityContentBrowseRecordService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.ActivityContentBrowseRecordView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName ActivityContentBrowseRecordController
 * @Description TODO
 * @Date 2021/2/7 23:23
 * @Author maixy
 */
@RestController
@RequestMapping("/v1/activity/content/browse/record")
public class ActivityContentBrowseRecordController extends ApiController {

    @Resource
    private ActivityContentBrowseRecordService activityContentBrowseRecordService;

    /**
     * 添加浏览记录
     *
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse addBrowseRecord(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");
        Long activityContentId = request.getDataParamAsLong("activityContentId");

        if (null == activityId ||  null == activityContentId) {
            throw new MissParamException();
        }
        activityContentBrowseRecordService.addBrowseRecord(userId, activityId, activityContentId);
        return ApiResponse.ok();
    }

    /**
     * 获得浏览记录列表
     *
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getBrowseRecord(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");
        Page page = Page.validation(request.getPage());

        if (null == activityId) {
            throw new MissParamException();
        }
        PageData<ActivityContentBrowseRecordView> pageData = activityContentBrowseRecordService.getBrowseRecord(userId, activityId, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

}
